package cn.edu.gcu.giftems.daos;

import cn.edu.gcu.giftems.pojos.Order;
import cn.edu.gcu.giftems.pojos.User;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import java.util.List;

/**
 * 用户Dao类.
 *
 * @author ZhengHQ
 * @create 2018-09-24
 * @see
 */
public class UserDao extends HibernateDaoSupport {

    public User findByUsernameAndPassword(User user) {
        String hql = "from User u where u.name =: name and u.pwd =: pwd";
        //利用事务模板对象进行数据库的操作
        String[] paramName= new String[]{"name", "pwd"};
        String[] value= new String[]{user.getName(), user.getPwd()};
        List<User> list = (List<User>)this.getHibernateTemplate().findByNamedParam(hql,paramName,value);
        //对list集合进行判断
        if(list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    //获取用户总记录
    public int findCount() {
        String hql = "select count(*) from User";
        //List list=new ArrayList();
        List<Long> list = (List<Long>)this.getHibernateTemplate().find(hql);
        if(list.size() > 0){
            return list.get(0).intValue();
        }
        return 0;
    }
//
//    //获取用户总记录
//    public int findCountByParameter(String filter_name, String simple_filter_value) {
//        List<Long> list = null;
//            list = (List<Long>)this.getHibernateTemplate()
//                    .find("select count(*) from User where "
//                            + filter_name + " like ?0", "%"+simple_filter_value + "%");
//        //List list=new ArrayList();
//        //List<Long> list = (List<Long>)this.getHibernateTemplate().find(hql);
//        if(list.size() > 0){
//            return list.get(0).intValue();
//        }
//        return 0;
//    }

    //获取用户集合信息
    public List<User> findByPage(int begin, int pageSize) {
        DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
        List<User> list = (List<User>)this.getHibernateTemplate().findByCriteria(criteria, begin, pageSize);
        return list;
    }
    //添加用户
    public void save(User user) {
        this.getHibernateTemplate().save(user);
    }
    
    //根据用户编号查询用户信息
    public User finById(int id) {
        return this.getHibernateTemplate().get(User.class,id);
    }
    
    //更新用户信息
    public void update(User user) {
        this.getHibernateTemplate().update(user);
    }
    
    //根据用户id删除用户
    public void delete(User user) {
        this.getHibernateTemplate().delete(user);
    }

    public List<User> findAll() {
        String hql = "from User";
        return (List<User>)this.getHibernateTemplate().find(hql);
    }

    public User findPassword(User user) {
        String hql = "from User u where u.name =: name and u.phone =: phone";
        //利用事务模板对象进行数据库的操作
        String[] paramName = new String[]{"name", "phone"};
        String[] value = new String[]{user.getName(), user.getPhone()};
        List<User> list = (List<User>)this.getHibernateTemplate().findByNamedParam(hql,paramName,value);
        //对list集合进行判断
        if(list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    public List<User> findByParameter(String filter_name, String simple_filter_value) {
        List<User> list = null;
            String hql = "from User u where " + filter_name + " like '%" + simple_filter_value + "%'";
            try {
                list = (List<User>) this.getHibernateTemplate().execute(
                        new HibernateCallback<Object>() {
                            public Object doInHibernate(Session arg0)
                                    throws HibernateException {
                                Query query = arg0.createQuery(hql);
                                return query.list();
                            }
                        }
                );
            } catch (RuntimeException re) {
                throw re;
            }
        return list;
    }
}
